# Below is the interface for Iterator, which is already defined for you.


class Iterator:
    def __init__(self, nums):
        """
        Initializes an iterator object to the beginning of a list.
        :type nums: List[int]
        """
        pass

    def hasNext(self):
        """
        Returns true if the iteration has more elements.
        :rtype: bool
        """
        pass

    def next(self):
        """
        Returns the next element in the iteration.
        :rtype: int
        """
        pass


class PeekingIterator:
    """这个不知道为啥有问题,没看出来"""
    def __init__(self, iterator):
        self.iterator = iterator
        self.peek = None

    def peek(self):
        if not self.peek:
            self.peek = self.iterator.next()
        return self.peek

    def next(self):
        if self.peek:
            mid, self.peek = self.peek, None
            return mid
        else:
            return self.iterator.next()

    def hasNext(self):
        if self.peek:
            return True
        else:
            try:
                return self.iterator.hasNext()
            except:
                return False


class PeekingIterator:
    def __init__(self, iterator):
        self.memory = []
        while iterator.hasNext():
            self.memory.append(iterator.next())
        self.index = 0
        self.max_length = len(self.memory)

    def peek(self):
        return self.memory[self.index]

    def next(self):
        mid = self.memory[self.index]
        self.index += 1
        return mid

    def hasNext(self):
        return self.index < self.max_length


# Your PeekingIterator object will be instantiated and called as such:
# iter = PeekingIterator(Iterator(nums))
# while iter.hasNext():
#     val = iter.peek()   # Get the next element but not advance the iterator.
#     iter.next()         # Should return the same value as [val].
# Exception: Type <class 'generator'>: Not implemented
# Line 105 in serialize (./python3/__serializer__.py)
# Line 67 in __helper_select_method__ (Solution.py)
# Line 88 in __helper__ (Solution.py)
# Line 122 in _driver (Solution.py)
# Line 131 in <module> (Solution.py)